$(function(){
	$("input[name='is_aptitude']").change(function(){
		var val = $(this).val();
		if(parseInt(val) === 1){
			app.showAptitude();
		}else{
			app.isAptitudeShow = false;
			$("#label_list_aptitude").html('');
			$("#label_more_aptitude").hide();
			$("#aptitude_desc_box").hide();
		}
	});
	
	$("#aptitudeApp .aptitude-box").removeClass("hide");
})

var app = new Vue({
	el:"#aptitudeApp",
	delimiters: ['[[', ']]'],
	data:{
		isAptitudeShow:false,
		aptitudeList:window.aptitudeData,
		childrenSelect:null,
		childrenIndex:-1,
	},
	computed:{
		resultList:function(){
			var list = [];
			this.aptitudeList.forEach(function(item,index){
				if(item.children && item.children.length>0){ //有子项目
					item.children.forEach(function(childrenItem,childrenIndex){
						if(childrenItem.selected){    //子项目被选中
							list.push({
								id:childrenItem.id,
								index:index,
								childrenIndex:childrenIndex,
								text:childrenItem.text
							});
						}
					});
				}else{
					if(item.selected){ //没有子项目，自身被选中
						list.push({
							id:item.id,
							index:index,
							text:item.text
						});
					}
				}
			})
			return list;
		}
	},
	methods:{
		showAptitude:function(){
			var arr = [];
			$("#label_list_aptitude span").each(function(){
				arr.push(parseInt($(this).data('code')))
			});
			
			//对aptitudeList进行初始化,判断项目是否被选中
			this.aptitudeList.forEach(function(item){
				if(item.children && item.children.length>0){ 
					item.children.forEach(function(childrenItem){ //含有子项目，子项目包含这个id，childrenItem.selected = true
						if($.inArray(childrenItem.id,arr) > -1){
							childrenItem.selected = true;
						}else{
							childrenItem.selected = false;
						}
					})
				}else{
					if($.inArray(item.id,arr) > -1){ //一级包含这个id，item.selected = true
						item.selected = true;
					}else{
						item.selected = false;
					}
				}
			});
			
			this.childrenSelect = null;//从一级开始选择
			this.isAptitudeShow = true;
			$("#label_more_aptitude").show();
			$("#aptitude_desc_box").show();
		},
		removeItem:function(item){
			if(item.childrenIndex){ //是一个子项目时，需要定位父级
				this.aptitudeList[item.index].children[item.childrenIndex].selected = false;
			}else{
				this.aptitudeList[item.index].selected = false;
			}
		},
		itemClick:function(index){
			var item = this.aptitudeList[index];
			if(item.children && item.children.length>0){ //有子项目，切换到子项目选择器
				this.childrenSelect = item.text;
				this.childrenIndex = index;
			}else{ //无子项目，改变状态
				item.selected = !item.selected
			}
		},
		childrenItemClick:function(index){
			var item = this.aptitudeList[this.childrenIndex].children[index];
			item.selected = !item.selected;
		},
		showAptitudeList:function(){ // 进入到一级选择列表
			this.childrenSelect = null;
		},
		isActive:function(item){
			if(item.children && item.children.length==0){
				return{
					active:item.selected
				}
			}
			if(item.children && item.children.length>0){
				var flag = false,i = 0,l=item.children.length;
				for (;i<l;i++) {  //判断子选项中是否有选中的
					if(item.children[i].selected){
						flag = true;
						break;
					}
				}
				return{
					active:flag
				}
			}
		},
		childrenSelectedLength:function(item){
			var selected = 0,i = 0,l=item.children.length;
			for (;i<l;i++) {  //判断子选项中是否有选中的
				if(item.children[i].selected){
					selected++
				}
			}
			return selected == 0 ? false : selected;
		},
		closeAptitudeBox:function(){
			this.isAptitudeShow = false;
			this.checkHasSelected();
		},
		setFinalResult:function(){ //设置最终的结果
			var _html = '';
			this.resultList.forEach(function(item){ //将选中的项目设置到finalResult
				_html += '<span class="svae_box_label aui-titlespan" data-class="aptitude" data-code="'+item.id+'" data-name="'+item.text+'" onclick="removespan_label(this)"><input name="label[aptitude][]" value="'+item.id+'" type="hidden">'+item.text+'<i>×</i></span>'
			});
			$("#label_list_aptitude").html(_html);
			this.isAptitudeShow = false;
			this.checkHasSelected();
		},
		checkHasSelected:function(){
			if($("#label_list_aptitude span").length == 0){
				$("input[name='is_aptitude']").eq(1).prop("checked",true)
				$("#label_more_aptitude").hide();
				$("#aptitude_desc_box").hide();
			}
		}
	}
})